//################################################################################
// MIT License
// Copyright (c) 2024 ZhangYihua
//
// Change Logs:
// Date           Author       Notes
// 2020-10-18     ZhangYihua   first version
//
// Description  : 
//################################################################################

`ifndef FILE_SIM_GLB_SV
`define FILE_SIM_GLB_SV

class RGRS_MNG;
    integer     chk_num_max;
    integer     chk_num_cur;
    string      tc_name;
    string      msg[];

    function new(string tc_name, integer chk_num_max);
        this.tc_name = tc_name;
        this.chk_num_max = chk_num_max;

        chk_num_cur = 0;

        msg = new[chk_num_max];
    endfunction

    function void one_chk_done(string msg);
        if (chk_num_cur<chk_num_max) begin
            $display("check %0d completed at %0t:%s", chk_num_cur, $time(), msg);

            this.msg[chk_num_cur] = msg;
            chk_num_cur = chk_num_cur + 1;
        end else begin
            $display("one check completed at %0t:%s. however it's over chk_num_max", $time(), msg);
        end
    endfunction

    task wait_chks_done(time runtime_max);
        integer     i;
`ifdef REGRESS_MODE
        integer     file;

        file = $fopen("regress_status.txt", "a");
        $fdisplay(file, "%s START -->", tc_name);
        $fclose(file);
`else
        $display("%s START -->", tc_name);
`endif

        fork
            begin
                wait(chk_num_cur>=chk_num_max);
`ifdef REGRESS_MODE
                file = $fopen("regress_status.txt", "a");
                $fdisplay(file, "   %s END normally with all checks done at time %0t.\n", tc_name, $time());
                $fclose(file);
`else
                $display("   %s END normally with all checks done at time %0t.\n", tc_name, $time());
`endif
            end

            begin
                #runtime_max;
`ifdef REGRESS_MODE
                file = $fopen("regress_status.txt", "a");

                for (i=0;i<chk_num_cur;i=i+1) begin
                    $fdisplay(file, "   %0d/%0d:%s", (i+1), chk_num_max, msg[i]);
                end

                $fdisplay(file, "%s FAIL because of overtime at time %0t.\n", tc_name, $time());
                $fclose(file);
`else
                for (i=0;i<chk_num_cur;i=i+1) begin
                    $display("   %0d/%0d:%s", (i+1), chk_num_max, msg[i]);
                end

                $display("%s run time out at time %0t.\n", tc_name, $time());
`endif
            end
        join_any

`ifdef REGRESS_MODE
        $finish;
`endif
    endtask

endclass

`endif
